




//
//  EquipmentListViewController.m
//  nncloud
//
//  Created by macfeel on 2019/5/27.
//  Copyright © 2019 macfai. All rights reserved.
//

#import "EquipmentListViewController.h"

@interface EquipmentListViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *equipArray;

@property(nonatomic,strong)UITextField *textField;

@end

@implementation EquipmentListViewController
#define cellIdentifier  @"relayd"
//#define HeaderHight  290
#define PageSize 20

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            [ws loadData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.navigationItem setItemWithTitle:@"设备名称列表" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.equipArray = [NSMutableArray array];
    
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"新增名称" Image:@"" selectedIMG:@"" tartget:self action:@selector(addNew)];
    
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view.
}
///新增名称
-(void)addNew{
    ///
    [self addMingcheng];
}

///新增名称
-(void)addMingcheng
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请添加名称" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    self.textField = [UITextField new];
    self.textField.placeholder = @"请输入";
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = self.textField.placeholder;
        textField.keyboardType = 0;
        self.textField = textField;
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        return ;
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        [self addManufacturer];
        [self saveAdd];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

#pragma 添加设备归属的大类的名称
///保存新增
-(void)saveAdd{
    NSString *url;
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        url = kAddRelayTypeNameUrl;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        url = kAddSwitchTypeNameUrl;
    }else  if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        url = kAddCMTypeNameUrl;
    }else{
        url = kAddCamelTypeNameUrl;
    }
    
    NSDictionary *dict = @{@"relayType":self.textField.text?:@"",};
    [NNetworkClient postRequestWithBaseUrl:url setParameters:dict success:^(id responseObject) {
        if ([responseObject[@"status"] integerValue]==0) {
            [AFToast showText:@"添加成功"];
        }
    } fail:^(NSError *error) {
    } delegater:nil];
}


#pragma mark - 获取数据
-(void)loadData{
    ///选择设备类型
    NSString *url;
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        url = kGetRelayEquipmentTypeUrl;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        url = kGetSwitchListUrl;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        url = kGetCMListUrl;
    }else{
        url = kGetCamelListUrl;
    }
    [NNetworkClient postRequestWithBaseUrl:url setParameters:@{} success:^(id responseObject) {
        
        YYLog(@"%@",responseObject);
        if ([responseObject[@"status"] integerValue]==0) {
            NSArray *arr = responseObject[@"data"];
            for (NSDictionary *dict in arr) {
                CommonModel *model = [CommonModel mj_objectWithKeyValues:dict];
                [self.equipArray addObject:model];
            }
            [self.tableView reloadData];
        }
    } fail:^(NSError *error) {
    } delegater:nil];
}



#pragma mark - tableView   delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.equipArray.count;
    //    NSDictionary *dict = self.dict;
    //    return [dict allKeys].count;
    //    return self.nameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.equipArray[indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }
    //    NSDictionary *noteDictionary = [self.notes objectAtIndex: indexPath.row];
    //    cell.textLabel.text = noteDictionary[@"title"];
    //    cell.detailTextLabel.text = [self.dates objectAtIndex: indexPath.row];
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.selectionStyle = 0;
    cell.textLabel.textColor = kDarkGrayColor;
//  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell.textLabel.text = self.nameArray[indexPath.row];
    
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        cell.textLabel.text = model.equipmentName;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        ///转辙机
        cell.textLabel.text = model.qicaiName;
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        
        ///综合器材
        cell.textLabel.text = model.qicaiZhonglei;
    }else{
        cell.textLabel.text = model.camelTypeName;
    }
    
    //cell.detailTextLabel.text = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CommonModel *model = self.equipArray[indexPath.row];
    
    [NUserModel shareInstance].currentModel = model;
    
    ///这个数据是为了去获取对应继电器 下的型号列表
    
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        
        ///relay
        [NUserModel shareInstance].relayName = model.xuhao;
        
        if (self.block) {
            self.block(model.xuhao);
        }
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        ///转辙机
        if (self.block) {
            ///传的是名称
            self.block(model.qicaiName);
            [NUserModel shareInstance].switchName = model.qicaiName;
        }
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        
        if (self.block) {
            ///传的是名称
            self.block(model.qicaiZhonglei);///上个页面需要展示的名字
            [NUserModel shareInstance].CMName = model.qicaiZhonglei;
        }
        
    }else{
        if (self.block) {
            ///传的是名称
            self.block(model.camelTypeName);///上个页面需要展示的名字
            [NUserModel shareInstance].camelName = model.camelTypeName;
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}


@end
